package com.taobao.base.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 获取当前用户身份工具类
 */

public class SecurityUtil {

    public static TbUser getUser() {
        try {
            Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principalObj instanceof String) {
                //取出用户身份信息
                String principal = principalObj.toString();
                //将json转成对象
                TbUser user = JSON.parseObject(principal, TbUser.class);
                return user;
            }
        } catch (Exception e) {
            System.err.println("获取当前登录用户身份出错:" + e.getMessage());
            e.printStackTrace();
        }

        return null;
    }


    @Data
    public static class TbUser implements Serializable {

        private static final long serialVersionUID = 1L;

        private int id;
        private String tbId;
        private String username;
        private String password;
        private String tbName;
        private String headimgurl;
        private String sex;
        private String email;
        private String cellphone;
        private String qq;

        /**
         * 用户状态
         */
        private String status;

        private LocalDateTime createTime;

        private LocalDateTime updateTime;


    }


}